<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h2>直接通过FormData</h2>
    <!-- e.target.files[0] -->
    <!-- 推荐用户选择 图片 -->
    <input type="file" accept="image/*" />
    <img src="" alt="" />
    <!-- 点击按钮 上传用户选择的图片 -->
    <button>上传</button>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

    <script>
        document.querySelector('button').addEventListener('click',function(){
            const data = new FormData()
            //  获取用户选择的文件
            const img = document.querySelector('input').files[0]
            data.append('avatar',img)
            axios({
                url:'http://ajax-api.itheima.net/api/file',
                method:'post',
                data,
            }).then( res => {
                // console.log(url)
                document.querySelector('img').src = res.data.data.url
            })
           
        })
    </script>
</body>
</html>